<template>
	<view>


		<!-- 新建工地弹窗 -->
		
		
		<form @submit="addSubmit">
			<view class="addPop_box">
				<view class="peopleBox">
					<view :class="'peopleItem '+(peopleType==1?'active':'') " @click="peopleTypeSelect(1)">
						<view class="peopleIcon">
							<image src="/static/peopleType1.png" mode=""></image>
						</view>
						<view class="peopleMain">
							我是工人
							<text>给自己记工、记账</text>
						</view>
						<view class="peopleSelect" v-if="peopleType==1">
							<u-icon name="checkbox-mark" color="#fff" size="22"></u-icon>
						</view>
					</view>
					<view :class="'peopleItem '+(peopleType==2?'active':'')" @click="peopleTypeSelect(2)">
						<view class="peopleIcon">
							<image src="/static/peopleType2.png" mode=""></image>
						</view>
						<view class="peopleMain">
							我是带班
							<text>给工人记工、记账</text>
						</view>
						<view class="peopleSelect" v-if="peopleType==2">
							<u-icon name="checkbox-mark" color="#fff" size="22"></u-icon>
						</view>
					</view>
				</view>

				<view class="handleList_box">
					<view class="handleList_con">
						<view class="handleLabel handleLabelLong">项目名称：</view>
						<view class="handleList">
							<view class="pickerCon">
								<input type="decimal" name="name" placeholder="请输入工地名称"
									placeholder-class="placeholderColor">
							</view>
						</view>
					</view>
					
					<view class="tckToDownOne tckTop">
						<text>记工方式</text>
						<view class="tckOneItemBox tckTop">
							<view  :class="'tckOneItem '+(workType==0?'typeActive':'')" @click="selectWorkType(0)">
								按工
							</view>
							<view :class="'tckOneItem '+(workType==1?'typeActive':'')" @click="selectWorkType(1)">
								按小时
							</view>
						</view>
					</view>
					<!-- #ifdef APP-PLUS -->
					<view class="handleList_con marginTop">
						<view class="handleLabel handleLabelLong">项目地址：</view>
						<view class="handleList">
							<view class="pickerCon" @click="getLocation">
								<input disabled :value="address" placeholder="请选择项目地址"
									placeholder-class="placeholderColor">
								<u-icon name="map-fill" color="#999" size="40"></u-icon>
							</view>
						</view>
					</view>
					<!-- #endif -->
					
				</view>
			</view>
			<view class="handlePost_con">
				<button class="completeBtn" form-type="submit" hover-class="none">确 认</button>
			</view>
		</form>

	</view>
</template>

<script>
	export default {
		data() {
			return {
				// 默认工地信息
				defaultWorksite: JSON.parse(uni.getStorageSync('defaultWorksite')),

			

				// 工地列表
				worksiteList: [],
				in_num: 0,
				out_num: 0,

				// 新建弹窗
				addShow: false,
				projectInfo: {},
				project_id: 1,

				// 默认
				defaultChecked: false,
				delShow: false,
				editShow: false,

				projectAddShow: false,
				peopleType: 1,
				workType: 0,

				// 地址
				address: '',
				longitude: '',
				latitude: '',

				// 表单
				formData: {},
			}
		},
		onLoad(option) {
			// uni.showLoading({
			// 	title: '加载中'
			// });
			if (option.type == 'quick') {
				this.projectAddShow = true;
			}
		},
		onShow() {
		},
		onReady() {
			this.getLocation();
		},
		
		onHide() {
			this.addShow = false;
		},
		methods: {
			// 通过组件定义的ref调用uni-popup方法 ,如果传入参数 ，type 属性将失效 ，仅支持 ['top','left','bottom','right','center']
			moveHandle() {
				return false
			},

			// 选择身份
			peopleTypeSelect(type) {
				this.peopleType = type
			},
			
			selectWorkType(type){
				this.workType=type
			},

			//获取定位信息
			getLocation() {
				// console.log(mapKey);
				let _this = this;
				uni.getLocation({
					type: 'wgs84',
					// geocode: true,
					success: function(res) {
						_this.longitude = res.longitude;
						_this.latitude = res.latitude;
			
						// 设置经纬度缓存
						uni.setStorageSync('longitude', res.longitude);
						uni.setStorageSync('latitude', res.latitude);
			
						// 引入腾讯地图SDK核心类
						var QQMapWX = require('@/util/qqmap-wx-jssdk.min.js');
						var qqmapsdk = new QQMapWX({
							key: uni.getStorageSync('mapKey'),
						});
			
						// 根据经纬度获取所在位置
						qqmapsdk.reverseGeocoder({
							location: {
								longitude: res.longitude,
								latitude: res.latitude,
							},
							success: function(res) {
								// 设置城市缓存
								if(res.result.address){
									uni.setStorageSync('province', res.result.ad_info.province);
									uni.setStorageSync('city', res.result.ad_info.city);
									uni.setStorageSync('area', res.result.ad_info.district);
									uni.setStorageSync('address', res.result.address);
									_this.address=res.result.address
								}
								
							}
						});
					},
					fail: function(res) {
					}
				});
			},

			// 提交工地
			addSubmit(e) {
				// console.log(e);
				var _this = this;
				var formData = e.detail.value;
				var kindObj = _this.kindObj;

				if (!formData.name) {
					uni.showModal({
						content: '项目名称不能为空',
						showCancel: false,
					})
					return;
				}
				_this.formData = formData;
				_this.$u.throttle(_this.submitAddForm, 3000);
			},
			// 提交接口
			submitAddForm() {
				var _this = this;
				_this.$u.post('/api/project.index/add_do', {
					role: _this.peopleType,
					name: _this.formData.name,
					address: uni.getStorageSync('address'),
					work_type: _this.workType || '',
					lng:  uni.getStorageSync('longitude'),
					lat:  uni.getStorageSync('latitude'),
					
				}).then(res => {
					console.log(res);
					uni.showToast({
						title: res.msg,
						duration: 2000
					});

					//更新一下缓存工地
					_this.getDefaultWorksite();
					setTimeout(function() {
						uni.navigateBack()
					}, 2000)
				}).catch(res => {
					
				})
			},

			// 获取默认工地
			getDefaultWorksite() {
				var _this = this;
				_this.$u.get('/api/project.index/getDefault', {}).then(res => {
					console.log('==获取默认工地==');
					var defaultWorksite = res.data;
					// 缓存默认工地信息
					uni.setStorageSync('defaultWorksite', JSON.stringify(defaultWorksite));
			
				})
			},
			

			

			

		}
	}
</script>
<style>
	page{
		background-color: #fff;
	}
</style>
<style scoped lang="scss">
	

	// 新建工地弹窗
	.addPop_box {
		padding: 0 30rpx;
		padding-top: 50rpx;
	}

	.addPop_tit {
		padding: 40rpx 0;
		font-size: 32rpx;
		font-weight: bold;
		color: $uni-color;
		text-align: center;
		overflow: hidden;
	}

	.peopleBox {
		padding-bottom: 30rpx;
		border-bottom: 1rpx solid #eee;
		overflow: hidden;
		display: flex;
	}

	.peopleItem {
		flex: 1;
		height: 160rpx;
		padding: 40rpx 20rpx;
		border: 2rpx solid #F0F0F0;
		border-radius: 10rpx;
		color: $uni-color;
		font-size: 28rpx;
		line-height: 40rpx;
		position: relative;
	}

	.peopleItem.active {
		border: 2rpx solid $uni-color-active;
	}

	.peopleItem:last-child {
		margin-left: 20rpx;
	}

	.peopleIcon {
		width: 70rpx;
		height: 70rpx;
		float: left;
		margin-right: 10rpx;
	}

	.peopleIcon image {
		width: 100%;
		height: 100%;
	}

	.peopleMain text {
		display: block;
		color: $uni-color-text;
		font-size: 24rpx;
	}

	.peopleSelect {
		position: absolute;
		top: 0;
		right: 0;
		border-radius: 0 10rpx 0 30rpx;
		background-color: #327FE8;
		width: 40rpx;
		height: 40rpx;
		text-align: center;
	}

	.handleList_box {
		padding: 15rpx 0;
	}
	
	

	
	.tckToDownOne{
		width: 100%;
		overflow: hidden;
		line-height: 90rpx;
		padding-top: 20rpx;
	}
	.tckOneItemBox{
		width: 540rpx;
		float: right;
	}
	.tckOneItem{
		height: 90rpx;
		line-height: 90rpx;
		text-align: center;
		margin: 0;
		padding: 0;
		background:#f0f0f0;
		border-radius: 10rpx;
		width: 200rpx;
		float: left;
		margin-right: 20rpx;
	}
	.typeActive{
		background:$uni-color-active;
		color: #fff;
	}
	
	.pickerCon input{
		padding: 6rpx 0;
	}
	
	.handlePost_con{
		border-top: 0;
		margin-top: 100rpx;
	}
	.marginTop{
		margin-top: 20rpx;
	}
</style>
